package sy.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.log4j.Logger;

import sy.dao.SysAccountDaoI;
import sy.model.PerAddress;
import sy.model.SysAccount;
import sy.service.SysAccountServiceI;
import sy.util.Encrypt;

@Service("sysAccountService")
public class SysAccountServiceImpl implements SysAccountServiceI {
	
	private static final Logger logger = Logger.getLogger(SysAccount.class);
	private SysAccountDaoI sysAccountDao;


	public SysAccountDaoI getSysAccountDao() {
		return sysAccountDao;
	}

	@Autowired
	public void setSysAccountDao(SysAccountDaoI sysAccountDao) {
		this.sysAccountDao = sysAccountDao;
	}

	@Override
	public Serializable save(SysAccount sysAccount) {
		String passwordMD5 = Encrypt.md5(sysAccount.getPassword());
		sysAccount.setPassword(passwordMD5);
		return sysAccountDao.save(sysAccount);
	}

	@Override
	public void remove(String[] ids) {
		// TODO Auto-generated method stub
		for (int i = 0; i < ids.length; i++) {
			SysAccount sysAccount =	sysAccountDao.getSysAccountById(ids[i]);
			sysAccountDao.remove(sysAccount);
		}
	
	}

	/* (non-Javadoc)
	 * @see sy.service.SysAccountServiceI#update(sy.model.SysAccount)
	 */
	@Override
	public void update(SysAccount sysAccount) {
		// TODO Auto-generated method stub
		sysAccountDao.update(sysAccount);
	}

	/* (non-Javadoc)
	 * @see sy.service.SysAccountServiceI#list()
	 */
	@Override
	public List<SysAccount> list() {
		// TODO Auto-generated method stub
		return sysAccountDao.list();
	}

	/* (non-Javadoc)
	 * @see sy.service.SysAccountServiceI#getSysAccountById(java.lang.String)
	 */
	@Override
	public SysAccount getSysAccountById(String id) {
		// TODO Auto-generated method stub
		return sysAccountDao.getSysAccountById(id);
	}

	/* (non-Javadoc)
	 * @see sy.service.SysAccountServiceI#listSysAccountByKey(java.lang.String)
	 */
	@Override
	public List<SysAccount> listSysAccountByKey(String key) {
		// TODO Auto-generated method stub
		return sysAccountDao.listSysAccountByKey(key);
	}
	
	/* (non-Javadoc)
	 * @see sy.service.SysAccountServiceI#listSysAccountByKey(java.lang.String)
	 */
	@Override
	public Object login(String name, String password) {
		// TODO Auto-generated method stub
		SysAccount result= sysAccountDao.findByNameAndPwd(name, password);
		return result;
	}

	/* (non-Javadoc)
	 * @see sy.service.SysAccountServiceI#listSysAccountByKey(java.lang.String)
	 */
	@Override
	public Object findByName(String telephone) {
		// TODO Auto-generated method stub
		SysAccount result= sysAccountDao.findByName(telephone);
		return result;
	}

	@Override
	public SysAccount getSysAccountByToken(String token) {
		// TODO Auto-generated method stub
		return sysAccountDao.getSysAccountByToken(token);
	}

	@Override
	public void updateLoginTime(String token) {
		// TODO Auto-generated method stub
		sysAccountDao.updateLoginTime(token);
	}

	@Override
	public void modifyPassword(String telephone, String password) {
		// TODO Auto-generated method stub
		sysAccountDao.modifyPassword(telephone, password);
	}
}
